package lampachat_client;

import java.io.IOException;

/**
 *
 * @author alex
 */
public class JRegistration extends javax.swing.JFrame {

    /**
     * Creates new form JRegistration
     */
    JHome home;
    public JRegistration(JHome home) {
        this.home=home;
        initComponents();
        jLabelRegistered.setVisible(false);
        jLabelLoginFree.setVisible(false);
        jLabelLoginBusy.setVisible(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public boolean singUp() {
        String[] stringToSearch = {";", "\"", " ", "+", "\\"};
        String login = jTextField1.getText();
        String password = jTextField2.getText();
        for (String s : stringToSearch) {
            if (login.contains(s) || login.contains(s)) {
                System.out.println("fail pass- ");
                return false;
            }
        }
        Message message = new Message();
        System.out.println("Jregist > singUP()");
        String res = message.sendServiceSingUp(login, password);
        if (res.equals("You are registered!\n\nPlease RESTART app!")) {
            Database database = new Database();
            database.setLoginAndPass(login, password);
            new JInfoWindow(home).setText(res);
            return true;
        } else if (res.equals("Username is busy")) {
            jLabelSearch.setVisible(false);
            jLabelLoginBusy.setVisible(true);
            return false;
        } else {
            new JInfoWindow().setText(res);
            return false;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabelLoginBusy = new javax.swing.JLabel();
        jLabelLoginFree = new javax.swing.JLabel();
        jLabelRegistered = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabelSearch = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registration");
        setMinimumSize(new java.awt.Dimension(231, 180));
        setPreferredSize(new java.awt.Dimension(300, 190));
        setResizable(false);
        setSize(new java.awt.Dimension(300, 190));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setOpaque(false);
        jPanel1.setPreferredSize(new java.awt.Dimension(231, 180));
        jPanel1.setLayout(null);

        jLabel1.setText("Login");
        jLabel1.setPreferredSize(new java.awt.Dimension(70, 18));
        jPanel1.add(jLabel1);
        jLabel1.setBounds(100, 10, 50, 20);

        jLabel2.setText("Password");
        jLabel2.setPreferredSize(new java.awt.Dimension(100, 17));
        jPanel1.add(jLabel2);
        jLabel2.setBounds(75, 66, 100, 17);

        jTextField1.setPreferredSize(new java.awt.Dimension(150, 23));
        jPanel1.add(jTextField1);
        jTextField1.setBounds(138, 6, 150, 23);

        jTextField2.setPreferredSize(new java.awt.Dimension(150, 23));
        jPanel1.add(jTextField2);
        jTextField2.setBounds(138, 63, 150, 23);

        jLabelLoginBusy.setFont(new java.awt.Font("Liberation Sans", 1, 18)); // NOI18N
        jLabelLoginBusy.setForeground(new java.awt.Color(204, 0, 0));
        jLabelLoginBusy.setText("Login is busy");
        jPanel1.add(jLabelLoginBusy);
        jLabelLoginBusy.setBounds(170, 30, 117, 22);

        jLabelLoginFree.setFont(new java.awt.Font("Liberation Sans", 1, 18)); // NOI18N
        jLabelLoginFree.setForeground(new java.awt.Color(0, 217, 0));
        jLabelLoginFree.setText("Login is FREE!!!");
        jPanel1.add(jLabelLoginFree);
        jLabelLoginFree.setBounds(150, 30, 137, 22);

        jLabelRegistered.setFont(new java.awt.Font("Liberation Sans", 0, 24)); // NOI18N
        jLabelRegistered.setForeground(new java.awt.Color(0, 217, 0));
        jLabelRegistered.setText("You are Registered!");
        jLabelRegistered.setFocusable(false);
        jPanel1.add(jLabelRegistered);
        jLabelRegistered.setBounds(40, 100, 247, 28);
        jLabelRegistered.getAccessibleContext().setAccessibleName("jLabelRegistered");

        jButton1.setText("REGISTER NOW");
        jButton1.setPreferredSize(new java.awt.Dimension(150, 40));
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(6, 98, 288, 40);

        jLabelSearch.setFont(new java.awt.Font("Liberation Sans", 1, 18)); // NOI18N
        jLabelSearch.setForeground(new java.awt.Color(102, 102, 102));
        jLabelSearch.setText("Search...");
        jPanel1.add(jLabelSearch);
        jLabelSearch.setBounds(210, 30, 76, 22);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        getContentPane().add(jPanel1, gridBagConstraints);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lampachat_client/thumb_multicolor-stripes.jpg"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(jLabel5, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        System.out.println("Jregistr > button registr");
        if (singUp()) {
            jLabelSearch.setVisible(false);
            jButton1.setVisible(false);
            jLabelRegistered.setVisible(true);
        }
    }//GEN-LAST:event_jButton1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JRegistration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JRegistration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JRegistration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JRegistration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
       // java.awt.EventQueue.invokeLater(new Runnable() {
         //   public void run() {
           //     new JRegistration().setVisible(true);
            //}
        //});
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelLoginBusy;
    private javax.swing.JLabel jLabelLoginFree;
    private javax.swing.JLabel jLabelRegistered;
    private javax.swing.JLabel jLabelSearch;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
